package com.qz.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 计算器工具类
 * @Author Flag
 * @Date: 2021/7/20 21:26
 * @Version: 1.0
 **/
public class CalculatorUtils {

    private CalculatorUtils() {
    }


    private static final List<Character> operationList = new ArrayList<>();

    static {
        operationList.add('+');
        operationList.add('-');
        operationList.add('*');
        operationList.add('/');
        operationList.add('(');
        operationList.add(')');
        operationList.add('[');
        operationList.add(']');
        operationList.add('{');
        operationList.add('}');
    }

    /**
     * 返回运算符的优先级，优先级是程序员来确定的，优先级使用数字表示
     * 数字越大，优先级越高
     * @param oper 操作符
     * @return
     */
    public static int priority(int oper){
        if (oper == '*' || oper == '/'){
            return 1;
        } else if (oper == '+' || oper == '-'){
            return 0;
        } else {
            //假定目前的表达式只有+ - * /
            return -1;
        }
    }

    public static int priority(String oper){
        if ("*".equals(oper) || "/".equals(oper)){
            return 1;
        } else if ("+".equals(oper) || "-".equals(oper)){
            return 0;
        } else {
            //假定目前的表达式只有+ - * /
            return -1;
        }
    }

    /**
     * 判断是不是一个运算符
     * @param val
     * @return
     */
    public static boolean isOper(char val){
        return operationList.contains(val);
    }

    /**
     * 判断是否是数字
     * @param val
     * @return
     */
    public static boolean isNumber(char val){
        return (val >= 48 && val <= 57) || val == 46;
    }


    /**
     * 判断是否是左侧括号
     * @param val
     * @return
     */
    public static boolean isLeftBracket(char val){
        return val == '(' || val =='[' || val =='{';
    }

    public static boolean isLeftBracket(String val){
        return "(".equals(val) || "[".equals(val) || "{".equals(val);
    }

    /**
     * 判断是否是右侧括号
     * @param val
     * @return
     */
    public static boolean isRightBracket(char val){
        return val == ')' || val ==']' || val =='}';
    }

    public static boolean isRightBracket(String val){
        return ")".equals(val) || "]".equals(val) || "}".equals(val);
    }

    public static int bracketEquals(char oper){
        if(oper == '{' || oper == '}'){
            return 4;
        } else if (oper == '[' || oper == ']'){
            return 3;
        }  else if (oper == '(' || oper == ')'){
            return 2;
        } else {
            new RuntimeException(oper+"is not a bracket");
        }
        return 1;
    }


    public static int bracketEquals(String oper){
        if("{".equals(oper) || "}".equals(oper)){
            return 4;
        } else if ("[".equals(oper) || "]".equals(oper)){
            return 3;
        }  else if ("(".equals(oper) || ")".equals(oper)){
            return 2;
        } else {
            new RuntimeException(oper+"is not a bracket");
        }
        return 1;
    }
    /**
     * 计算
     * @param num1 数字一
     * @param num2 数字二
     * @param oper 运算符
     * @return 计算结果
     */
    public static int cal(int num1,int num2,int oper){

        //用于存储计算的结果
        int result = 0;
        switch (oper){
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num2 - num1;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num2 / num1;
                break;
            default:
                break;

        }
        return result;
    }

    /**
     * 计算
     * @param num1 数字一
     * @param num2 数字二
     * @param oper 运算符
     * @return 计算结果
     */
    public static double cal(double num1,double num2,int oper){

        //用于存储计算的结果
        double result = 0;
        switch (oper){
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num2 - num1;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num2 / num1;
                break;
            default:
                break;
        }
        return result;
    }
}
